﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OpticalPowerMeter.UserControls
{
    /// <summary>
    /// CheckControl.xaml 的交互逻辑
    /// </summary>
    public partial class CheckControl : UserControl
    {
        public EventHandler<CheckControlClickedArgs> ButtonClicked = null;

        public CheckControl()
        {
            InitializeComponent();
        }

        #region 绑定属性
        //字号
        static DependencyProperty MyFontSizeProperty = DependencyProperty.Register(
            "MyFontSize",
            typeof(double),
            typeof(CheckControl)
            );

        //各控件高度
        static DependencyProperty BoxHeightProperty = DependencyProperty.Register(
            "BoxHeight",
            typeof(double),
            typeof(CheckControl)
            );

        //输入框宽度
        static DependencyProperty BoxWidthProperty = DependencyProperty.Register(
            "BoxWidth",
            typeof(double),
            typeof(CheckControl)
            );

        //第1个控件的边距
        static DependencyProperty FirstMarginProperty = DependencyProperty.Register(
            "FirstMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第2个控件边距
        static DependencyProperty SecondMarginProperty = DependencyProperty.Register(
            "SecondMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第3个控件边距
        static DependencyProperty ThirdMarginProperty = DependencyProperty.Register(
            "ThirdMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第4个控件边距
        static DependencyProperty FourthMarginProperty = DependencyProperty.Register(
            "FourthMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第5个控件边距
        static DependencyProperty FifthMarginProperty = DependencyProperty.Register(
            "FifthMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第6个控件边距
        static DependencyProperty SixthMarginProperty = DependencyProperty.Register(
            "SixthMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第7个控件边距
        static DependencyProperty SeventhMarginProperty = DependencyProperty.Register(
            "SeventhMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //第8个控件边距
        static DependencyProperty EighthMarginProperty = DependencyProperty.Register(
            "EighthMargin",
            typeof(Thickness),
            typeof(CheckControl)
            );

        //检查点功率
        static DependencyProperty CheckPowerProperty = DependencyProperty.Register(
            "CheckPower",
            typeof(string),
            typeof(CheckControl)
            );

        //规格上限
        static DependencyProperty LimitHProperty = DependencyProperty.Register(
            "LimitH",
            typeof(string),
            typeof(CheckControl)
            );

        //规格下限
        static DependencyProperty LimitLProperty = DependencyProperty.Register(
            "LimitL",
            typeof(string),
            typeof(CheckControl)
            );
        #endregion

        #region 值
        /// <summary>
        /// 检查的功率点
        /// </summary>
        public string CheckPower
        {
            set { SetValue(CheckPowerProperty, value.ToString()); }
            get {
                try
                {
                    string text = ((string)GetValue(CheckPowerProperty)).Trim();
                    Convert.ToDouble(text);
                    return text;
                }
                catch
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 规格上限
        /// </summary>
        public string Limit_H
        {
            set { SetValue(LimitHProperty, value.ToString()); }
            get {
                try
                {
                    string text = ((string)GetValue(LimitHProperty)).Trim();
                    Convert.ToDouble(text);
                    return text;
                }
                catch
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 规格下限
        /// </summary>
        public string Limit_L
        {
            set { SetValue(LimitLProperty, value.ToString()); }
            get {
                try
                {
                    string text = ((string)GetValue(LimitLProperty)).Trim();
                    Convert.ToDouble(text);
                    return text;
                }
                catch
                {
                    return "";
                }
            }
        }
        #endregion

        private void Size_Changed(object sender,SizeChangedEventArgs args) {
            double thisHeight = ActualHeight; //控件的尺寸
            double thisWidth = ActualWidth;

            double controlSplit = 3; //子控件与子控件之间间隔5
            double fontsize1 = (thisHeight - 10) / 1.27; //根据高度计算的字号
            double fontsize2 = (thisWidth - controlSplit * 9 - 60) / 19.231; //根据宽度计算的字号
            double fontSize = fontsize1 < fontsize2 ? fontsize1 : fontsize2; //取两个字号中小的

            SetValue(MyFontSizeProperty, fontSize); //设置字号
            double height = fontSize * 1.27; //高度
            double width = fontSize * 2.443 + 10; //宽度
            SetValue(BoxHeightProperty, height); //控件高度
            SetValue(BoxWidthProperty, width); //控件宽度

            double firstLeft = controlSplit;
            SetValue(FirstMarginProperty,new Thickness(firstLeft,0,0,0)); //第一个控件边距

            double secondLeft = firstLeft + fontSize * 3 + 10 + controlSplit;
            SetValue(SecondMarginProperty,new Thickness(secondLeft,0,0,0));  //2

            double thirdLeft = secondLeft + fontSize * 2.443 + 10 + controlSplit;
            SetValue(ThirdMarginProperty,new Thickness(thirdLeft,0,0,0)); //3

            double fourthLeft = thirdLeft + fontSize * 3.181 + 10 + controlSplit;
            SetValue(FourthMarginProperty, new Thickness(fourthLeft,0,0,0)); //4

            double fifthLeft = fourthLeft + fontSize * 2.443 + 10 + controlSplit;
            SetValue(FifthMarginProperty, new Thickness(fifthLeft,0,0,0)); //5

            double sixthLeft = fifthLeft + fontSize * 3.181 + 10 + controlSplit;
            SetValue(SixthMarginProperty,new Thickness(sixthLeft,0,0,0)); //6

            double seventhLeft = sixthLeft + fontSize * 2.443 + 10 + controlSplit;
            SetValue(SeventhMarginProperty,new Thickness(seventhLeft,0,0,0)); //7

            double eighthLeft = seventhLeft + height + controlSplit/2;
            SetValue(EighthMarginProperty, new Thickness(eighthLeft,0,0,0)); //8

        }

        //加、减按钮点击时，产生一个事件
        private void Button_Click(object sender,RoutedEventArgs args) {
            Button button = sender as Button;
            string buttonName = button.Name;
            CheckControlClickedArgs checkControlClickedArgs = new CheckControlClickedArgs();
            if (buttonName.Equals("Add"))
            {
                checkControlClickedArgs.ChangeType = "Add";
            }
            else
            {
                checkControlClickedArgs.ChangeType = "Remove";
            }
            ButtonClicked?.Invoke(this, checkControlClickedArgs);
        }

    }
}
